<template>
  <XtxBread>
      <XtxBreadItem to='/'>首页</XtxBreadItem>
      <XtxBreadItem v-if="cate.top" :to="`/category/${cate.top.id}`">{{cate.top.name}}</XtxBreadItem>
     <transition name="fade-right" mode="out-in">
        <XtxBreadItem  v-if="cate.sub" :key='cate.sub.name'>{{cate.sub.name}}</XtxBreadItem>
     </transition>
  </XtxBread>
</template>

<script>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'SubBread',
  setup () {
    const store = useStore()
    const route = useRoute()
    const cate = computed(() => {
      // cate 用于存储 顶级类目 和 二级类目 的名字和id
      const tempCate = {}
      store.state.category.list.forEach(top => {
        // console.log(top)
        if (top.children) {
          top.children.forEach(sub => {
            if (sub.id === route.params.id) {
              // 顶级类目
              tempCate.sub = { id: sub.id, name: sub.name }
              // 二级类目
              tempCate.top = { id: top.id, name: top.name }
            }
          })
        }
      })

      return tempCate
    })

    return { cate }
  }
}
</script>

<style>

</style>
